package com.hongyuan.coach.ui.home.view_mode;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.coach.base.CustomViewModel;
import com.hongyuan.coach.data.Constants;
import com.hongyuan.coach.data.MyRepository;
import com.hongyuan.coach.myview.select_time.ContentTimeBean;
import com.hongyuan.coach.ui.home.beans.TopTimeBean;
import java.util.List;
public class EditTimeViewMode extends CustomViewModel<MyRepository> {



    //头部时间轴集合
    public ObservableField<List<TopTimeBean>> topTimes = new ObservableField<>();
    //内容时间
    public ObservableField<List<ContentTimeBean>> contentTimes = new ObservableField<>();
    //选中的日期字符串
    public ObservableField<String> nowData = new ObservableField<>();

    public EditTimeViewMode(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //改变头部时间轴ui变化
        public SingleLiveEvent<List<TopTimeBean>> changeTop = new SingleLiveEvent<>();
        //改变教练空闲时间ui
        public SingleLiveEvent<List<ContentTimeBean>> changeContent = new SingleLiveEvent<>();
    }


    //拨打电话
    public BindingCommand submit = new BindingCommand(() -> {

    });

    /*
     * 更改头部时间选中状态
     * */
    public void changeTab(int position){
        for(int i = 0 ; i < topTimes.get().size() ; i++){
            if(i == position){
                topTimes.get().get(i).setSelect(true);
            }else{
                topTimes.get().get(i).setSelect(false);
            }
        }

        uc.changeTop.setValue(topTimes.get());
    }

    /*
     * 获取选中的开始时间或者结束时间
     * */
    public String getSelectDT(int mPosition){
        return nowData.get() + " "+contentTimes.get().get(mPosition).getNow_time();
    }

    /*
    * 获取所选时间的内容
    * */
    public void reqTimes(String day){
        setParams("day",day).requestData(Constants.GET_COACH_TIMEPLAN_DAY_LIST_COACH);
    }

    /*
     * 查找今日
     * */
    private String getTodayPosition(List<TopTimeBean> mList){
        for(int i = 0 ; i < mList.size() ; i++){
            if(mList.get(i).getIs_cur_date() == 1){
                topTimes.get().get(i).setSelect(true);
                nowData.set(mList.get(i).getNow_day());
                return mList.get(i).getNow_day();
            }
        }
        return mList.get(0).getNow_day();
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_PLAN_DATA){
            List<TopTimeBean>  mList = (List<TopTimeBean>) dataBean;
            topTimes.set(mList);
            uc.changeTop.setValue(topTimes.get());
            reqTimes(getTodayPosition(mList));
        }

        if(code == Constants.GET_COACH_TIMEPLAN_DAY_LIST_COACH){
            List<ContentTimeBean> list = (List<ContentTimeBean>) dataBean;
            contentTimes.set(list);

            uc.changeContent.setValue(list);
        }

        if(code == Constants.ADD_COACH_TIMEPLAN || code == Constants.DEL_COACH_TIMEPLAN){
            reqTimes(nowData.get());
        }
    }

}
